package com.main.dao;

import java.util.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.generico.base.BaseDaoImpl;
import com.generico.exception.AsiWebException;
import com.main.java.Inventario;
import com.main.java.Requisicion;

@Transactional
@Repository
public class InventarioDaoImpl extends BaseDaoImpl implements InventarioDao{
	@Autowired
	private SessionFactory sessionFactory;

	private Session openSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> obtenerInventario() {
		Criteria criteria = openSession().getSessionFactory().getCurrentSession().createCriteria(Inventario.class);
		criteria.createAlias("ctgMateriaPrimaId","ctgMateriaPrimaId", JoinType.INNER_JOIN);
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("inventariId")).
				add(Projections.property("ctgMateriaPrimaId.ctgCatalogoNombre")).
				add(Projections.property("inventarioCantidad")).
				add(Projections.property("ctgMateriaPrimaId.ctgCatalogoId")));
		criteria.add(Restrictions.eq("ctgMateriaPrimaId.ctgCatalogoActivo", "1"));
		criteria.addOrder(Order.asc("ctgMateriaPrimaId.ctgCatalogoNombre"));
		try {
			return (List<Object[]>) findByCriteria(criteria);
		} catch (AsiWebException e) {
			logger.error(e, e);
			return Collections.emptyList();
		}
	}
	
	public void guardarOActualizarInventario(Inventario inventario) throws AsiWebException{
		save(inventario);
	}
	
	public Inventario obtenerInventarioPorId(Long inventarioId) throws AsiWebException {
		Inventario inventario = new Inventario();
		inventario = (Inventario) findById(Inventario.class, inventarioId);
			return inventario;
	}
	
	public List<Object[]> obtenerInventarioParaControlInventario() {
		Criteria criteria = openSession().getSessionFactory().getCurrentSession().createCriteria(Inventario.class);
		criteria.createAlias("ctgMateriaPrimaId","ctgMateriaPrimaId", JoinType.INNER_JOIN);
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("inventariId")).
				add(Projections.property("ctgMateriaPrimaId.ctgCatalogoId")).
				add(Projections.property("inventarioCantidad")).
				add(Projections.property("ctgMateriaPrimaId.ctgCatalogoId")).
			
				add(Projections.property("ctgMateriaPrimaId.ctgCatalogoNombre")));
		criteria.add(Restrictions.eq("ctgMateriaPrimaId.ctgCatalogoActivo", "1"));
		criteria.addOrder(Order.asc("ctgMateriaPrimaId.ctgCatalogoNombre"));
		try {
			return (List<Object[]>) findByCriteria(criteria);
		} catch (AsiWebException e) {
			logger.error(e, e);
			return Collections.emptyList();
		}
	}
	
}
